package threading;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-03-18
 * Time: 22:18
 */
public class ThreadDemo4 {
      public static  int  ret1=0;
      public  static   int ret2=0;//没有一个读入一个修改可以不加volitale;
    public static void main(String[] args) throws InterruptedException {
        int array[]=new int[10000000];
        long startTime=System.currentTimeMillis();
        Random random=new Random();
        for(int i=0;i<array.length;i++){
            array[i]=(1+random.nextInt(100));
        }
        Thread t1=new Thread(()->{
            for(int i=0;i<array.length;i++){
                ret1+=array[i];
            }
            System.out.println("t1"+ret1);
        });
        Thread t2=new Thread(()->{
            for(int i=1;i<array.length;i++){
                ret2+=array[i];
            }
            System.out.println("t2"+ret2);
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long endTime=System.currentTimeMillis();
        int sum=ret1+ret2;
        System.out.println("结果"+sum);
        System.out.println("执行时间"+(endTime-startTime));


    }
}
